
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bill_stats extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
		$this->load->library('PHPexcel');

	}

	public function index(){
		$this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	 	 $this->load->view($this->session->userdata('formain'),array(
	 	 	'acct' => $this->bill_stats_model->get_account()
	 	 ));
	  	$this->load->view('layouts/footer');
	}

	public function get_umr(){
		echo json_encode($this->bill_stats_model->get_bill($this->input->post('acct_num')));
	}

	public function gen_pdf() {
		ob_start(); 
		//$date = $_GET['year'].'-'.$_GET['month'];
		$result = $this->bill_stats_model->get_status($_GET['bill_id']);
		$inv = $this->bill_stats_model->get_inv($_GET['bill_id']);
		$bill_mon = $_GET['bill_mon'];
		$bill_yr = $_GET['bill_year'];
		$month = $bill_mon  - 1;
        $mons = $month;
        $year = $bill_yr;
        if($mons == 0){
        $year = $bill_yr - 1;
        $month = 12;
        }
		$tmp = explode('20',$year);
	    //var_dump($tmp[1].($month < 10 ? "0".$month : $month).$_GET['acct_num']);
		$bal = $this->bill_stats_model->get_bal($tmp[1].($month < 10 ? "0".$month : $month).$_GET['acct_num']);
		//var_dump($result);
		$table_data = '';
		$table_data2 = '';
		$table_data3 = '';
		$table_data4 = '';
		//<td style='border-bottom:1px solid; text-align: center;width=8%; padding: 5px;'>Year</td>
		//<td style="padding-right: 5px; padding-left: 5px; padding-top: 5px;" align="right">' . $key->year . '</td>
		foreach($result as $r){
			$area = $r->zone_1;
			$account_num = $r->acct_num;
			$account_name = $r->acct_name;
			$read_date = $r->reading_date;
			$bill_date = $r->bill_date;
			$bill_bal = $r->bill_bal;
			$due_date = $r->due_date;
			$prev_date = $r->prev_date;
			$amount_due = $r->total_due;
		}
		$remain_bal = 0;
		$tots = 0;
		//var_dump($bal);
		foreach ($bal as $i) {
			//$remain_bal = $bill_bal - $i->pay_amount;
			$table_data2 = '<tr><td style="text-align: left;padding-right: 5px; padding-left: 100px; padding-top: 5px;">Amount Due From Previous Bill</td>
			<td style="text-align: right;padding-right: 5px; padding-left: 5px; padding-top: 5px;">'.number_format($i->curchg, 2, ".", "," ).'</td></tr>';
		}
		foreach ($bal as $i) {
			//$remain_bal = $bill_bal - $i->pay_amount;
			$table_data3 .= '<tr><td style="text-align: left;padding-right: 5px; padding-left: 100px; padding-top: 5px;">Payment ('.$i->receipt_date.')</td>
			<td style="text-align: right;padding-right: 5px; padding-left: 5px; padding-top: 5px;">'.number_format($i->pay_amount, 2, ".", "," ).'</td></tr>';
			$tots += $i->pay_amount;
		}
		foreach ($bal as $i) {
			$remain_bal = $i->curchg - $tots;
			$table_data4 = '<tr><td style="text-align: left;padding-right: 5px; padding-left: 100px; padding-top: 5px;">Remaining Balance From Previous Bill<br><i style="text-align: center; padding-top: 5px; font-size: 12px;">Payment as of </i></td>
			<td style="text-align: right;padding-right: 5px; padding-left: 5px; padding-top: 5px;">'.number_format($bill_bal, 2, ".", "," ).'</td>
			</tr>';
		}

		$total_cur_chg = 0;
		foreach ($inv as $i) {
			switch ($i->charge_code) {
				case 'WATCONS':
					$code = "Water Consumption";
					break;
				case 'SURCHG':
					$code = "Surcharge";
					break;
				case 'LPAYCHG':
					$code = "Late Payment Charge";
					break;
			}
			$table_data .= '<tr>
			<td style="text-align: left;padding-right: 5px; padding-left: 100px; padding-top: 5px;">'.$code.'</td>
			<td style="text-align: right;padding-right: 5px; padding-left: 5px; padding-top: 5px;">'.number_format($i->line_amt, 2, ".", "," ).'</td></tr>';
			$total_cur_chg += $i->line_amt;
		}
		$content = "
		<page>
        	<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=100%; padding-top:-5px;'>
						<h4>BRGY LUMBIA WATER SYSTEM</h4>
						<h4>Lumbia, Cagayan de Oro City</h4>
					</td>
				</tr>
			</table><br><br>
			<table style='width: 100%; padding-top:-10px;'>
	            <tr>
			        <td style='text-align: center;width=100%; padding-top:-5px;'>
						<h4>Bill Statement</h4>
						from: <b>".$prev_date."</b> to: <b>".$read_date."</b>
					</td>
				</tr>
			</table><br><br>
			<table style='width: 100%;'>
				<tr>
			        <td style='text-align: right;width=30%;'>Bill ID</td>
			        <td style='text-align: left;width=70%;'><b>".$_GET['bill_id']."</b></td>
				</tr>
	            <tr>
			        <td style='text-align: right;width=30%;'>Account Number:</td>
			        <td style='text-align: left;width=70%;'><b>".$account_num."</b></td>
				</tr>
				<tr>
			        <td style='text-align: right;width=30%;'>Account Name:</td>
			        <td style='text-align: left;width=70%;'><b>".$account_name."</b></td>
				</tr>
				<tr>
			        <td style='text-align: right;width=30%;'>Area:</td>
			        <td style='text-align: left;width=70%;'><b>".$area."</b></td>
				</tr>
			</table><br><br>
			<table style='width: 100%;'>
				<tr>
			        <td style='text-align: right;width=30%;'>Bill Date</td>
			        <td style='text-align: left;width=70%;'><b>".$bill_date."</b></td>
				</tr>
	            <tr>
			        <td style='text-align: right;width=30%;'>Due Date:</td>
			        <td style='text-align: left;width=70%;'><b>".$due_date."</b></td>
				</tr>
				<tr>
			        <td style='text-align: right;width=30%;'>Amount Due:</td>
			        <td style='text-align: left;width=70%;'><b>".number_format($total_cur_chg + $bill_bal, 2, ".", "," )."</b></td>
				</tr>
			</table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-bottom:1px solid; text-align: center;width=60%; padding: 5px;'><b>Previous Bill Charge</b></td>
					<td style='border-bottom:1px solid; text-align: right;width=40%; padding: 5px;'><b>Amount</b></td>
				</tr>" .
			$table_data2.$table_data3.$table_data4
			. "</table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-bottom:1px solid; text-align: center;width=60%; padding: 5px;'><b>Current Bill Charge</b></td>
					<td style='border-bottom:1px solid; text-align: right;width=40%; padding: 5px;'><b>Amount</b></td>
				</tr>" .
			$table_data
			. "<tr>
					<td style='border-top:1px solid; text-align: center;width=60%; padding: 5px;'><b>Total Current Bill Charge</b></td>
					<td style='border-top:1px solid; text-align: right;width=40%; padding: 5px;'><b>".number_format($total_cur_chg, 2, ".", "," )."</b></td>
				</tr></table><br><br>
			<table cellspacing='0' style='width: 100%; position: relative;'>
				<tr>
					<td style='border-top:1px solid; text-align: center;width=60%; padding: 5px;'><b>Total Amount Due</b></td>
					<td style='border-top:1px solid; text-align: right;width=40%; padding: 5px;'><b>".number_format($total_cur_chg + $bill_bal, 2, ".", "," )."</b></td>
				</tr></table>
		</page>
		";
		require_once dirname(__FILE__) . '\..\..\..\assets\html2pdf\html2pdf.class.php';
		$html2pdf = new HTML2PDF('P', 'A4', 'en');
		$html2pdf->pdf->SetTitle('Bills');
		$html2pdf->WriteHTML($content);
		$pdf = $html2pdf->Output('','S');
		//$pdf = $html2pdf->Output('WATER_BILL_' . date("dmY_His") . '.pdf', 'S');
		$data = array(
			':p_file' => $pdf,
			':p_file_name' => 'WATER_BILL',
			':p_file_type'	=> 'pdf',
			':p_account_num' => $_GET['acct_num'],
			':p_account_id'	=> 1,
			':p_bill_id'=> $_GET['bill_id']
		);
		$this->wus->save_pdf($data);
	}

	public function add_form(){
		$this->load->view($this->session->userdata('foradd'));
	}
	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'));
	}
	public function view_form($id){
		$this->load->view($this->session->userdata('forview'));
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */ 